package com.dankegongyu.lib.common.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by zhangbenzhi on 2018/3/13.
 * <p>
 * 自定义单选按钮：
 */

public class MyRadioButton extends androidx.appcompat.widget.AppCompatRadioButton {


    private onRadioButtonTouchListener onRadioButtonTouch;

    public MyRadioButton(Context context) {
        super(context);
    }

    public MyRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            setChecked(!isChecked());
        }
        if (onRadioButtonTouch != null) {
            onRadioButtonTouch.onTouch(isChecked());
        }
        return true;
    }

    public void setOnRadioButtonTouch(onRadioButtonTouchListener onRadioButtonTouch) {
        this.onRadioButtonTouch = onRadioButtonTouch;
    }

    public interface onRadioButtonTouchListener {
        void onTouch(boolean isChecked);
    }
}
